package com.aye.notepad.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.Toast;

import com.aye.notepad.DatabaseModel.User;
import com.aye.notepad.R;

public class RegisterActivity extends AppCompatActivity {
    private EditText et_name, et_pwd, et_pwd2, et_phone,et_date;
    private RadioButton rb_man,rb_woman;
    private Switch cb_pwd,cb_pwd2;
    private static final String TAG = "RegisterActivity";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        init();
    }
    //点击显示按钮
    public void show(View v){
        if (cb_pwd.isChecked()==true){
            //如果选中，则显示密码
            et_pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        } else {
            //否则隐藏密码
            et_pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
        if (cb_pwd2.isChecked()==true){
            et_pwd2.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        } else {
            et_pwd2.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
    }
    //点击注册按钮时进行信息验证
    public void register2(View v) {
        String name = et_name.getText().toString().trim();
        String pwd = et_pwd.getText().toString();
        String pwd2 = et_pwd2.getText().toString();
        String phone = et_phone.getText().toString();
        String date = et_date.getText().toString();
        String sex = null;
        if (rb_man.isChecked() == true) {
            sex = "男";
        } else if (rb_woman.isChecked()==true){
            sex = "女";
        }
        Log.i(TAG, "register2: name:"+name+",pwd:"+pwd+"pwd2:"+pwd2+"phone:"+phone+"date:"+date+"sex:"+sex);
        if (name != null && pwd != null && phone != null && date != null && sex != null) {
            if (!pwd.equals(pwd2)){
                showMsg("两次输入密码不相同");
            }else {
                User user=new User(name,pwd,phone,date,sex);
                Log.i(TAG, "register2: "+user.toString());
                if (user.save()) {
                    showMsg("注册成功");
                    Log.i(TAG, "register2: "+user.toString());
                    startActivity(new Intent(this, LoginActivity.class));
                }else {
                    showMsg("注册失败");
                }
            }
            }else{
                showMsg("注册失败，有内容不合法");
        }
    }
    public void back(View v){
        finish();
    }
    private void showMsg(String s) {
        Toast.makeText(RegisterActivity.this,s,Toast.LENGTH_SHORT).show();
    }

    private void init() {
        et_name=findViewById(R.id.et_name);
        et_pwd=findViewById(R.id.et_pwd);
        et_pwd2=findViewById(R.id.et_pwd2);
        et_phone=findViewById(R.id.et_phone);
        rb_woman=findViewById(R.id.rb_woman);
        rb_man=findViewById(R.id.rb_man);
        et_date=findViewById(R.id.et_date);
        cb_pwd=findViewById(R.id.cb_pwd);
        cb_pwd2=findViewById(R.id.cb_pwd2);
    }
}
